//
//  NYTabBarController.m
//  Nooryes
//
//  Created by baken on 2020/2/18.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYTabBarController.h"

#import "XMGTabBar.h"
#import "UIImage+Render.h"
#import "NYRecommendViewController.h"
#import "NYNavigationViewController.h"
#import "NYInfluenceViewController.h"
#import "NYRemovedViewController.h"
#import "NYAccountViewController.h"
#import "NYArticleDetailViewController.h"

@interface NYTabBarController ()<UITabBarControllerDelegate>


@property (nonatomic, strong) NSString *pushID;

@property (nonatomic, strong) UIViewController *previousClickedController;

@property (nonatomic, strong) NYRecommendViewController *recommendViewController;

@end

@implementation NYTabBarController

+(void)load{
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UITabBar appearance] setTranslucent:NO];
//    [[UITabBar appearance] setTintColor:[UIColor redColor]];
//    [[UITabBar appearance] setUnselectedItemTintColor:UIColorFromRGB(0x888888)];
    
    if (@available(iOS 13, *)) {
        
    } else {
        UITabBarItem *item = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
        NSMutableDictionary *attr = [NSMutableDictionary dictionary];
        // 文本颜色 -> 描述富文本属性的key -> NSAttributedString.h
        attr[NSForegroundColorAttributeName] = [UIColor redColor];
        [item setTitleTextAttributes:attr forState:UIControlStateSelected];
        
        NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
        attrnor[NSFontAttributeName] = [UIFont systemFontOfSize:9];
        attrnor[NSForegroundColorAttributeName] = UIColorFromRGB(0x888888);
        [item setTitleTextAttributes:attrnor forState:UIControlStateNormal];
        [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
        [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
   
    
    
    // 添加所有子控制器
    [self addAllTabBarController];
    
    // 设置tabBar上对应按钮内容 -> 由对应的子控制器的tabBarItem 决定
    [self setupAllTitleButton];
    
    // 自定义tabBar
    [self setupTabBar];
    
    
    if (@available(iOS 13, *)) {
        UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
        appearance.shadowImage = [[UIImage alloc] init];
        appearance.backgroundImage = [[UIImage alloc] init];
        
        appearance.backgroundColor = [UIColor whiteColor];
        appearance.shadowColor = [UIColor clearColor];
        UITabBarItemAppearance *itemApp = [[UITabBarItemAppearance alloc] init];
        NSMutableDictionary *attr = [NSMutableDictionary dictionary];
        attr[NSForegroundColorAttributeName] = [UIColor redColor];
        attr[NSFontAttributeName] = [UIFont systemFontOfSize:9];

        NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
        attrnor[NSForegroundColorAttributeName] = UIColorFromRGB(0x888888);
        attrnor[NSFontAttributeName] = [UIFont systemFontOfSize:9];

        [itemApp.normal setTitleTextAttributes:attrnor];
        [itemApp.selected setTitleTextAttributes:attr];
        appearance.stackedLayoutAppearance = itemApp;
        self.tabBar.standardAppearance = appearance;
        
    } else {
    }
    
}

#pragma mark - 自定义tabBar
- (void)setupTabBar {
    XMGTabBar *tabBar = [[XMGTabBar alloc] init];
    // 替换系统的tabBar KVC:设置readonly属性
    [self setValue:tabBar forKey:@"tabBar"];
    
}

- (void)addAllTabBarController{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RecommendStoryboard" bundle:nil];
    NYRecommendViewController *recommendVC =[storyboard  instantiateViewControllerWithIdentifier:@"NYRecommendViewController"];
    NYNavigationViewController *reNaVC = [[NYNavigationViewController alloc] initWithRootViewController:recommendVC];
    [self addChildViewController:reNaVC];
    self.homeVC = recommendVC;
    NYRemovedViewController *removedVC = [[NYRemovedViewController alloc] initWithNibName:@"NYRemovedViewController" bundle:nil];
    NYNavigationViewController *removedNaviVC = [[NYNavigationViewController alloc] initWithRootViewController:removedVC];
    [self addChildViewController:removedNaviVC];
    
    NYInfluenceViewController *influVC = [[NYInfluenceViewController alloc] initWithNibName:@"NYInfluenceViewController" bundle:nil];
    NYNavigationViewController *influNaVC = [[NYNavigationViewController alloc] initWithRootViewController:influVC];
    [self addChildViewController:influNaVC];
    
    
    UIStoryboard *accountBoard = [UIStoryboard storyboardWithName:@"NYLoginStoryboard" bundle:nil];
    NYAccountViewController *accountVC = [accountBoard instantiateViewControllerWithIdentifier:@"NYAccountViewController"];
    NYNavigationViewController *accountNaviVC = [[NYNavigationViewController alloc] initWithRootViewController:accountVC];
    [self addChildViewController:accountNaviVC];
    
    //    _recommendViewController = recommendVC;
    //    self.previousClickedController = recommendVC;
}

#pragma mark - 设置所有标题按钮内容
- (void)setupAllTitleButton
{
    UINavigationController *nav = self.childViewControllers[0];
    nav.tabBarItem.title = @"推荐";
    nav.tabBarItem.image = [UIImage imageNamed:@"tab_tuijian_g"];
    nav.tabBarItem.selectedImage = [UIImage imageNameWithOriginal:@"tab_tuijian_r"];
    
    UINavigationController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"拔草";
    nav1.tabBarItem.image = [UIImage imageNamed:@"tab_bacao_g"];
    nav1.tabBarItem.selectedImage = [UIImage imageNameWithOriginal:@"tab_bacao_r"];
    
    UINavigationController *nav3 = self.childViewControllers[2];
    nav3.tabBarItem.title = @"种草";
    nav3.tabBarItem.image = [UIImage imageNamed:@"tab_zhongcao_g"];
    nav3.tabBarItem.selectedImage = [UIImage imageNameWithOriginal:@"tab_zhongcao_r"];
    
    UINavigationController *nav4 = self.childViewControllers[3];
    nav4.tabBarItem.title = @"我的";
    nav4.tabBarItem.image = [UIImage imageNamed:@"tab_wode_g"];
    nav4.tabBarItem.selectedImage = [UIImage imageNameWithOriginal:@"tab_wode_r"];
    
}



@end
